# -*- mode: python; -*-

#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
import os

Import('CfgmEnv')
env = CfgmEnv.Clone()

autogen_infra_sources = [
                 '#/src/contrail-api-client/generateds/generateDS.py',
                 '#/src/contrail-api-client/generateds/TypeGenerator.py',
                 '#/src/contrail-api-client/generateds/ccmap.py',
                 '#/src/contrail-api-client/generateds/idl_parser.py',
                 '#/src/contrail-api-client/generateds/ifmap_global.py',
                 '#/src/contrail-api-client/generateds/ifmap_model.py',
                 '#/src/contrail-api-client/generateds/ifmap_frontend.py',
                ]

autogen_sources = [
                 '#controller/src/config/device-manager/device_schema/juniper/juniper_common.xsd',
                ]

abstract_autogen_sources = [
                 '#controller/src/config/device-manager/device_schema/abstract_device/abstract_device.xsd',
                ]

autogen_script = File('#src/contrail-api-client/generateds/generateDS.py').path
autogen_cmd = '%s -f -o %s -g device-api' % (autogen_script, Dir('device_api/juniper_common').path)
abstract_gen_cmd = '%s -f -o %s -g device-api' % (autogen_script, Dir('abstract_device_api/abstract_device').path)

generated_files = [
                  'device_api/__init__.py',
                  'device_api/juniper_common_xsd.py'
                ]
generated_rule = env.Command(generated_files,
                             '#controller/src/config/device-manager/device_schema/juniper/juniper_common.xsd',
                             autogen_cmd + ' $SOURCE')

abstract_generated_files = [
                  'abstract_device_api/__init__.py',
                  'abstract_device_api/abstract_device_xsd.py'
                ]
abstract_generated_rule = env.Command(abstract_generated_files,
                             '#controller/src/config/device-manager/device_schema/abstract_device/abstract_device.xsd',
                             abstract_gen_cmd + ' $SOURCE')

setup_sources = [
           'setup.py',
           'MANIFEST.in',
           'run_tests.sh',
           'requirements.txt',
           'test-requirements.txt',
           '.testr.conf',
           '.coveragerc',
           'test',
          ]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(
        env.Install(Dir('.'), "#controller/src/config/device-manager/" + file))

local_sources = [
    'device_manager/__init__.py',
    'device_manager/dm_server.py',
    'device_manager/dm_server_args.py',
    'device_manager/device_manager.py',
    'device_manager/device_job_manager.py',
    'device_manager/device_ztp_manager.py',
    'device_manager/db.py',
    'device_manager/dm_utils.py',
    'device_manager/dm_amqp.py',
    'device_manager/device_conf.py',
    'device_manager/ansible_base.py',
    'device_manager/opserver_util.py',
    'device_manager/plugins/imports.py',
    'device_manager/plugins/ansible/ansible_conf.py',
    'device_manager/plugins/ansible/ansible_role_common.py',
    'device_manager/plugins/ansible/job_handler.py',
    'device_manager/plugins/ansible/overlay/overlay_conf.py',
    'device_manager/plugins/ansible/pnf/pnf_conf.py',
    'device_manager/plugins/juniper/juniper_conf.py',
    'device_manager/plugins/juniper/mx/mx_conf.py',
    'device_manager/plugins/juniper/mx-e2/e2_conf.py',
    'device_manager/plugins/juniper/mx-e2/e2_services_info.py',
    'device_manager/plugins/juniper/qfx/qfx_conf.py',
    'device_manager/plugins/juniper/qfx/series5K/qfx_5k.py',
    'device_manager/plugins/juniper/qfx/series10K/qfx_10k.py',
    'device_manager/logger.py',
]

local_sources_rules = []
for file in local_sources:
    local_sources_rules.append(
        env.Install("device_manager",
                    "#controller/src/config/device-manager/" + file))

generateds_rule = env.Install("device_api", "#src/contrail-api-client/generateds/generatedssuper.py")
abstract_generateds_rule = env.Install("abstract_device_api", "#src/contrail-api-client/generateds/generatedssuper.py")

dm_introspect_pkg = env.SandeshGenPy('dm_introspect.sandesh',
                                       'device_manager/sandesh/', False)

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_depends = [generated_rule, generateds_rule, abstract_generated_rule, abstract_generateds_rule]
sdist_depends.extend(setup_sources_rules)
sdist_depends.extend(local_sources_rules)
sdist_depends.extend(dm_introspect_pkg)
sdist_gen = env.Command('dist/device_manager-0.1dev.tar.gz', 'setup.py', cd_cmd + 'python setup.py sdist')

env.Depends(generated_rule, [autogen_infra_sources, autogen_sources])
env.Depends(abstract_generated_rule, [autogen_infra_sources, abstract_autogen_sources])
env.Depends(sdist_gen, sdist_depends)
env.Default(sdist_gen)

# Documentation
doc_files = []
doc_files += env.SandeshGenDoc('dm_introspect.sandesh')
doc_files += env['CFGM_DOC_FILES']

if 'install' in BUILD_TARGETS:
    install_cmd = env.Command(None, 'setup.py',
                              cd_cmd + 'python setup.py install %s' %
                              env['PYTHON_INSTALL_OPT'])
    env.Depends(install_cmd, sdist_depends)
    env.Alias('install', install_cmd)

env.Alias('install', env.Install(env['INSTALL_CONF'],
          'contrail-device-manager.conf'))
env.Alias('install', env.InstallAs(env['INSTALL_INITD'] +
          '/contrail-device-manager', 'contrail-device-manager.initd.supervisord'))
env.Alias('install', env.InstallAs(env['INSTALL_SYSTEMD'] +
          '/contrail-device-manager.service', 'systemd/contrail-device-manager.service'))
env.Alias('install', env.Install(env['INSTALL_CONF'] +
          '/supervisord_config_files', 'contrail-device-manager.ini'))
env.Alias('install', env.Install(
    env['INSTALL_MESSAGE_DOC'] + '/contrail-device-manager/', doc_files))

# XXX We may need to extend SetupPyTestSuite() to handle
#     override of default ':test' target, and/or add additional
#     targets to be specified
test_target = env.SetupPyTestSuite(sdist_gen,
                                   '/config/fabric-ansible/dist/fabric_ansible-0.1dev.tar.gz',
                                   '/config/api-server/dist/contrail-api-server-0.1dev.tar.gz',
                                   '/config/schema-transformer/dist/schema_transformer-0.1dev.tar.gz')

env.Alias('flaky-test', test_target)
env.Alias('controller/src/config/device-manager:flaky-test', test_target)

# Local Variables:
# mode: python
# End:
